#include "engine.h"
#include "bullet.h"
#include "minion.h"

Bullet::Bullet(int NewPower, double NewX, double NewY, double NewDestinationX, double NewDestinationY,
       Minion *NewTarget, double NewSpeed, void *NewPicture, BulletFlags NewFlags, int NewRange)
{
    X = NewX;
    Y = NewY;
    DestinationX = NewDestinationX;
    DestinationY = NewDestinationY;
    Power = NewPower;
    Target = NewTarget;
    Speed = NewSpeed;
    Picture = NewPicture;
    Flags = NewFlags;
    Range = NewRange;
}

Bullet *Bullet::Tick()
{
    if(Target && Target->IsDead()) Target = nullptr;

    if((Flags & GUIDED) && Target && !Target->IsInvisible())
    {
        DestinationX = Target->GetX();
        DestinationY = Target->GetY();
    }

    double Orientation = Engine::GetDestinationOrientation(X, Y, DestinationX, DestinationY);
    Engine::Move(X, Y, DestinationX, DestinationY, Orientation, Speed);

    if(equal(DestinationX, X, 1.0) && equal(DestinationY, Y, 1.0))
    {
        if(Range==0)
        {
            if(Target) Target->Damage(Power, Flags);
        }
        else
        {
            // area damage
        }
        return Engine::RemoveBullet(this);
    }
    return this;
}

void Bullet::MinionRemovalNotification(Minion *RemovedMinion)
{
    if(Target == RemovedMinion)
    {
        Target = nullptr;
    }
}

double Bullet::GetX() { return X; }
double Bullet::GetY() { return Y; }
Bullet *Bullet::GetNextBullet() { return NextBullet; }
